import '@/style/main.scss';
import 'swiper/dist/idangerous.swiper.css'
import 'aos/dist/aos.css';
import 'photoswipe/dist/photoswipe.css'
import 'photoswipe/dist/default-skin/default-skin.css'
import 'mediaelement/build/mediaelementplayer.min.css'

var userAgent = navigator.userAgent   // 获取浏览器信息
var IEReg = new RegExp('MSIE \\d+\\.\\d+;');   // 正则校验IE标识符
var IEMsg = IEReg.exec(userAgent)   // 获取字符串
var IEVersionNum = new RegExp('\\d+\\.\\d');   // 正则获取版本

// 是IE9，引入js文件
if(IEMsg && parseFloat(IEVersionNum.exec(IEMsg)) == 9){
  import('flexibility').then(({ default : flexibility }) => {
    flexibility(document.documentElement);
  })
  console.log('IE9引入')
}else{
  console.log('非IE9不引入')
}

if(module.hot){
  console.log(`重新刷新`);
  module.hot.accept(function () {
    console.log(`scss更新`);
    // 对更新过的 library 模块做些事情...
  });
}

Do.add('peity',{
  type : 'js',
  path : basedir + 'jquery.peity.min.js'
})
Do.add('validform',{
  type : 'js',
  path : basedir + 'validform.min.js'
});
Do.add('placeholder',{
  type : 'js',
  path : basedir + 'jquery.placeholder.min.js'
})
Do.add('gsap',{
  type : 'js',
  path : basedir + 'gsap-3.11.1.min.js'
})
Do.add('layer',{
  type : 'js',
  path : basedir + 'layer/layer.js',
  requires : ['layer-css']
});
Do.add('layer-css',{
  type : 'css',
  path : basedir + 'layer/theme/default/layer.css'
});

Do.add('lazyload',{
  type : 'js',
  path : basedir + 'jquery.lazyload.min.js'
})
Do.add('bmap',{
  type : 'js',
  path : 'https://api.map.baidu.com/api?v=2.0&ak=aOpsbwrpo297oX0y1ZG7VPnwBETTGH5H&callback=bmapCallback'
})


jQuery(document).ready(function($) {
  import('./jquery.peity.min.js').then(res => {
    $('.pie').peity('pie');
  })
  import('./jquery.placeholder.min.js').then(res => {
    $('input').placeholder();
  })
  import('./validform.min.js').then(res => {
    $('#myform').Validform({
      tiptype : function(msg,o,ctrl) {
        if(o.type == 3){
          console.log(`显示错误`,msg);
        }
        return false;
      },
      tipSweep : true,
      beforeSubmit(){
        console.log(`helloworld`);
        return false;
      }
    })
  })
  import('./jquery.lazyload.min.js').then(res => {
    $("img.lazyload").lazyload();
  })
  import('./gsap-3.11.1.min.js').then(({ gsap })=> {
    $('.box2').click(function() {
      gsap.to('.box2',{
        duration : 1,
        x : 100,
        y : 100
      })
    })
  })
  let obj = {
    a : 1,
    b : 2
  }
  import('aos')
  .then((AOS) => {
    AOS.init({
      mirror: true,
      once: true,
    });
  })

  import ('async-validator').then(({ default : AsyncValidator })=> {
    const descriptor = {
      username: [
        {
          required: true,
          message: '请填写用户名'
        },
        {
          min: 3,
          max: 10,
          message: '用户名长度为3-10'
        }
      ]
    }
    // 根据校验规则构造一个 validator
    const validator = new AsyncValidator(descriptor)
    const data = {
      username: 'username1111'
    }
    validator.validate(data, (errors, fields) => {
      console.log(errors)
    })
  })
  $('.box').click(function() {
    Do('layer',function() {
      layer.open({
        title: '在线调试'
      });
    })
  });

  import('mediaelement/build/mediaelement-and-player.js').then(res => {
    $('video').mediaelementplayer();
  })

  import('zrender').then(zrender => {
    console.log(zrender);
    let main = zrender.init(document.getElementById('zrender'),{
      width : 200,
      height : 200
    })
    let box = new zrender.Rect({
      shape : {
        width : 15,
        height : 15
      },
      style : {
        fill : 'red'
      }
    })
    main.add(box);
  });

  import('echarts').then(echarts => {
    let myChart = echarts.init(document.getElementById('echarts'));
    // 指定图表的配置项和数据
    var option = {
      title: {
        text: 'ECharts 入门示例'
      },
      tooltip: {},
      legend: {
        data: ['销量']
      },
      xAxis: {
        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
      },
      yAxis: {},
      series: [
        {
          name: '销量',
          type: 'bar',
          data: [5, 20, 36, 10, 10, 20]
        }
      ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
  });

  import('swiper/dist/idangerous.swiper.min.js').then(({ default:Swiper }) => {
    var mySwiper = new Swiper('.swiper-container', {
      autoplay: 5000,//可选选项，自动滑动
    })
  });

  //import('rx/dist/rx.lite.min.js').then(Rx => {
    //let s$ = new Rx.Subject();
    //setInterval(() => {
      //s$.onNext(+new Date());
    //},1e3);
    //s$.subscribe(res => {
      //console.log(res);
    //});
  //})


  $('#photo').click(function() {
    Promise.all([
      import('photoswipe/dist/photoswipe.min.js'),
      import('photoswipe/dist/photoswipe-ui-default.min.js'),
      import('@/js/pswp.js'),
    ]).then(([{ default : PhotoSwipe } ,{ default : PhotoSwipeUI_Default }])=> {
      var pswpElement = document.querySelectorAll('.pswp')[0];
      // build items array
      var items = [
          {
              src: 'https://placekitten.com/600/400',
              w: 600,
              h: 400
          },
          {
              src: 'https://placekitten.com/1200/900',
              w: 1200,
              h: 900
          }
      ];
      // define options (if needed)
      var options = {
          // optionName: 'option value'
          // for example:
          index: 0 // start at first slide
      };
      // Initializes and opens PhotoSwipe
      var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
      gallery.init();
    })
  });
  $('#mymap').click(function() {
    window.bmapCallback = () => {
      console.log(999);
      // 百度地图API功能
      var map = new BMap.Map("mymap");    // 创建Map实例
      map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);  // 初始化地图,设置中心点坐标和地图级别
      //添加地图类型控件
      map.addControl(new BMap.MapTypeControl({
        mapTypes:[
                BMAP_NORMAL_MAP,
                BMAP_HYBRID_MAP
            ]}));	  
      map.setCurrentCity("北京");          // 设置地图显示的城市 此项是必须设置的
      map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    }
    Do('bmap',function() {});
  })

});
